package handlers

import (
	"github.com/gin-gonic/gin"
	"social-hub1/models"
	"social-hub1/pkg"
)

func Register(c *gin.Context) {
	tel := c.PostForm("tel")
	password := c.PostForm("password")
	var user models.User
	if len(tel) != 11 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "手机号必须是11位",
		})
		return
	}
	err := models.DB.Where("tel=?", tel).First(&user).Error
	if err == nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "手机号已被注册",
		})
		return
	}
	data := models.User{
		Tel:      tel,
		Password: pkg.Md5(password),
	}

	err = models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "注册失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "注册成功",
	})
	return

}
